<?php
namespace Home\Controller;
use Think\Controller;
/*
 *总后台加盟商家z控制器
 */
class ShopAccountController extends HomeController {
	/*
	 * 充值提现列表（所有加盟商家的充值提现列表）
	 * 
	 */
	public function index()
	{
		//获取所有上架产品
		$page=I('get.page','1','strip_tags');
		$shop_account=M('shop_account a ');
		
		$count=$shop_account->join('inner join '.C('DB_PREFIX').'shop_user b on a.shopid=b.shopid')
		                     ->where('b.isdel=%d',0)
		                     ->count();
		 $pagearr= getPageArr($page,C('PAGESIZE'),$count);
		$result=$shop_account->join('inner join '.C('DB_PREFIX').'shop_user b on a.shopid=b.shopid')
		                     ->where('b.isdel=%d',0)
		                     ->limit($pagearr['start'],$pagearr['pagesize'])
		                     ->order('a.id desc')
		                     ->getField('a.id,a.shopid,a.account,a.add_time,a.admin_check,a.process_type,a.payment,a.admin_note,b.companyname');	
		$this->assign('pagearr',$pagearr);
		$this->assign('result',$result);
		$this->display('index');

	}
	/*
	 *编辑加盟商家账户
	 *@param shop  商家id 
	 *@param 此笔记录的ID 
	 */
	public function editAccount(){
		$shopid=I('get.shopid','','strip_tags');
        $id=I('get.id','','strip_tags');
        //获取商家名称
        $res=M('shop_user')->where('shopid=%d',$shopid)->getField('isdel');
        //已激活
        if(empty($res)){
        	 $shopname=M('shop_user')->where('shopid=%d',$shopid)->getField('companyname');
        	//获取此商家充值提现信息
       		$result= M('shop_account')->where('id=%d',$id)->select();
       		//获取支付方式
       		$payname=M('pay_ment')->where('id=%d',$result[0]['payment'])->getField('name');
       		$this->assign('payname',$payname);
       		$this->assign('shopname',$shopname);
       		$this->assign('account',$result[0]);

       		$this->display('editAccount');
        }else{
        	echo "<script> alert('商家已冻结，暂时充值提现');window.history.go(-1);</script>";
        }
        
       
	}
	/*
	 ×保存加盟商家充值审核并保存到商家账户记录表中（cart_account_log）
	 */
	public function updateAccount(){
			$accountid=I('post.accountid','','strip_tags');
			$process_type=I('post.process_type','','strip_tags');
			$data['admin_check']=I('post.admin_check','','strip_tags');
			if (empty($accountid)) {
				echo "<script> alert('参数错误');window.history.go(-1);</script>";
				return '';
			}
			//更改此笔充值或提现的审核状态
			$shop_account=M('shop_account');
			$result=$shop_account->where('id=%d',$accountid)->save($data);
            if ($result !== false) {
            	$map=$shop_account->where('id=%d',$accountid)->getField('id,shopid,admin_check,account,process_type');
            	if ($map[$accountid]['admin_check']==1)
            	 {
            		//此笔业务已审核充值/提现记录写入账户记录表中
            		$account_log=M('account_log');
            		$shop_user=M('shop_user');
            		$account_log->startTrans();
            		$temp['shopid']=$map[$accountid]['shopid'];
            		$temp['change_time']=time();
            		$temp['change_type']= intval($map[$accountid]['process_type']);
            		if ($temp['change_type'] ===0) {
            			//充值记录
            			$temp['shop_money']=$map[$accountid]['account'];
            			$temp['change_desc']='充值'.$temp['shop_money'];
            		}elseif ($temp['change_type'] ===1) {
            			$temp['shop_money']=-($map[$accountid]['account']);
            			$temp['change_desc']='提现'.$temp['shop_money'];
            		}
            		//保存数据库中
            		$res=$account_log->add($temp);

            		//计算account_log 中商家的金额，将金额保存shop_user 中
            		$shop_money= $account_log->where('shopid=%d',$temp['shopid'])->sum('shop_money');
            		$redata= $shop_user->where('shopid=%d',$temp['shopid'])->setField('money',$shop_money);
            		if (!empty($res) && $redata !== false)
            		 {
            			$account_log->commit();//成功则提交
            			//echo "<script> alert('修改成功');window.history.go(-1);</script>";
                        echo "<script> alert('修改成功');window.location.href='./index'</script>";
                        return '';
            		}else{
            			$account_log->rollback();//失败回滚
            			echo "<script> alert('系统繁忙');window.history.go(-1);</script>";
                        return '';
            		}

            	}else{
                     echo "<script> alert('修改成功');window.history.go(-1);</script>";
                }

            }else{
                echo "<script> alert('系统繁忙');window.history.go(-1);</script>";
            }

            
			
			

	}
}